package bbsimon.first.calc;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bbsimon.first.model.calc.*;

public class CalcServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws IOException, ServletException {
			PrintWriter out = res.getWriter();
			out.println("<html><head><title>result</title>"
					+ "</head><body>");
			String s = req.getParameter("number1");
			int num1;
			try{
				num1 = Integer.parseInt(s);
			}
			catch(NumberFormatException nfe){
				out.println("the first number you input is not a integer");
				out.println("</body></html>");
				return;
			}
			s = req.getParameter("number2");
			int num2;
			try{
				num2 = Integer.parseInt(s);
			}
			catch(NumberFormatException nfe){
				out.println("the second number you input is not a integer");
				out.println("</body></html>");
				return;
			}

			CalcModel calc = new CalcModel();
			int result1 = calc.add(num1, num2);
			int result2 = calc.sub(num1, num2);
			out.println("the result is <br>" + num1 + " + " + num2 + " = "
					+ result1 + "<br>");
			out.println(num1 + " - " + num2 + " = "
					+ result2 + "<br><br><a href=\"./\">click to go back</a></body></html>");
		}
}
